﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using System.Xml.XPath;
using System.IO;

namespace Common.Dynamics.GP.eConnect.Serialization
{
    public static class ActNumstMapper
    {
        private static string ConfigFileName = String.Empty;
        private static XDocument xdoc = null;

        public static void LoadConfigFile(string FileName)
        {
            if (String.IsNullOrEmpty(FileName)) throw new ArgumentNullException("FileName");
            if (!File.Exists(FileName)) throw new FileNotFoundException(FileName);

            ConfigFileName = FileName;

            xdoc = XDocument.Load(ConfigFileName);

        }

        public static IDictionary<string, string> BuildFirstSegMap()
        {
            if (xdoc.isNull()) throw new Exception("Must load Config file first;");

            var fstsegeles = xdoc.XPathSelectElements("/ACTNUMST/FIRSTSEG/ADD");
            IDictionary<string, string> Mapping = new Dictionary<string, string>();

            fstsegeles.ForEach(p => Mapping.Add(p.Attribute("key").Value, p.Attribute("value").Value));

            return Mapping;
        }

        public static IDictionary<string, string> BuildSecondSegMap()
        {
            if (xdoc.isNull()) throw new Exception("Must load Config file first;");

            var fstsegeles = xdoc.XPathSelectElements("/ACTNUMST/SECONDSEG/ADD");
            IDictionary<string, string> Mapping = new Dictionary<string, string>();

            fstsegeles.ForEach(p => Mapping.Add(p.Attribute("key").Value, p.Attribute("value").Value));

            return Mapping;
        }

        public static IDictionary<string, string> BuildThirdSegMap()
        {
            if (xdoc.isNull()) throw new Exception("Must load Config file first;");

            var fstsegeles = xdoc.XPathSelectElements("/ACTNUMST/THIRDSEG/ADD");
            IDictionary<string, string> Mapping = new Dictionary<string, string>();

            fstsegeles.ForEach(p => Mapping.Add(p.Attribute("key").Value, p.Attribute("value").Value));

            return Mapping;
        }
    }
}
